package com.powernode.spring6.test;

import com.powernode.spring6.bean.*;
import com.powernode.spring6.jdbc.MyDataSource1;
import com.powernode.spring6.jdbc.MyDataSource2;
import com.powernode.spring6.jdbc.MyDataSource3;
import com.powernode.spring6.service.CustomerService;
import com.powernode.spring6.service.OrderService;
import com.powernode.spring6.service.UserService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTestDI {
    public static final Logger logger = LoggerFactory.getLogger(SpringTestDI.class);

    @Test
    public void testSetDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = context.getBean("userServiceBean", UserService.class);
        userService.saveUser();
    }

    @Test
    public void testConstructorDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        CustomerService customerService = context.getBean("customerServiceBean", CustomerService.class);
        customerService.save();

        CustomerService customerServiceBean2 = context.getBean("customerServiceBean2", CustomerService.class);
        customerServiceBean2.save();

        CustomerService customerServiceBean3 = context.getBean("customerServiceBean3", CustomerService.class);
        customerServiceBean3.save();
    }

    @Test
    public void testSetDI2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-di.xml");
        OrderService orderService = context.getBean("orderServiceBean", OrderService.class);
        orderService.generate();

        OrderService orderServiceBean2 = context.getBean("orderServiceBean2", OrderService.class);
        orderServiceBean2.generate();
    }

    @Test
    public void testSimpleSetDI3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-di.xml");
        User userBean = context.getBean("userBean", User.class);
        logger.info(userBean.toString());
    }

    @Test
    public void testCascadeDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("cascade.xml");
        Student studentBean = context.getBean("studentBean", Student.class);
        logger.info(studentBean.toString());
        Clazz clazzBean = context.getBean("clazzBean", Clazz.class);
        logger.info(clazzBean.toString());
    }

    @Test
    public void testSetArrayDI3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-array.xml");
        Person personBean = context.getBean("personBean", Person.class);
        logger.info(personBean.toString());
    }

    @Test
    public void testSetCollectionDI4() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-collection.xml");
        Human humanBean = context.getBean("humanBean", Human.class);
        logger.info(humanBean.toString());
    }

    @Test
    public void testSetNullDI5() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-di.xml");
        Cat catBean = context.getBean("catBean", Cat.class);
        logger.info(catBean.toString());
        MathBean mathBean = context.getBean("mathBean", MathBean.class);
        logger.info(mathBean.toString());
    }

    @Test
    public void testSetPDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-p.xml");
        Dog dog = context.getBean("dogBean", Dog.class);
        logger.info(dog.toString());
    }

    @Test
    public void testSetCDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("set-c.xml");
        People people = context.getBean("peopleBean", People.class);
        logger.info(people.toString());
    }

    @Test
    public void testSetUtilDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-util.xml");
        MyDataSource1 myDataSource1 = context.getBean("mds1", MyDataSource1.class);
        logger.info(myDataSource1.toString());
        MyDataSource2 myDataSource2 = context.getBean("mds2", MyDataSource2.class);
        logger.info(myDataSource2.toString());
        MyDataSource3 myDataSource3 = context.getBean("mds3", MyDataSource3.class);
        logger.info(myDataSource3.toString());
    }

    @Test
    public void testAutowireDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-autowire.xml");
//        OrderService orderService = context.getBean("orderService", OrderService.class);
//        orderService.generate();
        CustomerService customerService = context.getBean("customerService", CustomerService.class);
        customerService.save();
    }

    @Test
    public void testContextDI() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-properties.xml");
        MyDataSource1 mds1 = context.getBean("mds1", MyDataSource1.class);
        logger.info(mds1.toString());
    }
}
